/*******************************
 * maum.c
 *
 * Source code for MAUM
 *
 ******************************/

#include "util.h"
#include "maum.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*********
 * Simple usage instructions
 *********/
void maum_usage(char* progname) {
	fprintf(stderr, "Usage: %s [options] [target]\n", progname);
	fprintf(stderr, "-f FILE\t\tRead FILE as a maumfile.\n");
	fprintf(stderr, "-h\t\tPrint this message and exit.\n");
	fprintf(stderr, "-n\t\tDon't actually execute commands, just print them.\n");
	exit(0);
}

/****************************** 
 * this is the function that, when given a proper filename, will
 * parse the maumfile and read in the rules
 ***************/
void parse_file(char* filename) {
	int leave = 0;
	char* token;
	char* line = malloc(160*sizeof(char));
	char* linebuf = malloc(160*sizeof(char));
	char* word = malloc(160*sizeof(char));
	FILE* fp = file_open(filename);
	printf("**********Start scan %s**********\n", filename);
	//while((line = file_getline(line, fp)) != NULL) {
	while(!feof(fp)){
		fgets(line, 160, fp);	//	read one line each time.
		
		if(feof(fp)) break;		//	jump out when end of file.
		strcpy(linebuf, line);
		
		token = strtok(linebuf, ",.\n\t");	//	get token.
		while(token){	// start exec
			strcpy(word, token);
			if(word[0] == '#'){
				token = strtok(NULL, " ,.\n\t");
				break;	
			}	//	skip all comment.
			char* test = ":";
			char* testscan = strstr(word, test);
			char* end = "end";
			char* endscan = strstr(word, end);
			if(testscan - word > 0){
				printf("***Start execue %s\n", word);
				token = strtok(NULL, " ,.\n\t");
				break;
			}	//	skip target line.
			else if(endscan - word == 0){
				printf("***end of target***\n");
				token = strtok(NULL, " ,.\n\t");
				break;
			}	//	skip end line.
			//printf("%c, %s\n",word[0], word);
			printf(">%s\n", word);
			char** prog;
			int numTokens;
			if ( (numTokens = makeargv(word, " ", &prog)) < 0) {
				perror("makeargv error");
				//exit(2);
				continue;
			}
			pid_t pid;
			int status = 0;
			
			pid = fork();
			if (pid == 0) { //child
				//exec
				
				execvp(prog[0], prog);	
				perror("exec failed");
				exit(status);
				exit(0);
			} 
			else if (pid > 0) { //parent
				wait(&status);
			} 
			else {
				perror("failed to fork");
			}
			token = strtok(NULL, " ,.\n\t");	//	get next token.
		}
		
	// this loop will go through the given file, one line at a time
	// this is where you need to do the work of interpreting
	// each line of the file to be able to deal with it later

	}
	printf("**********End of scanning %s**********\n", filename);
	fclose(fp);
	free(line);
}

int main(int argc, char* argv[]) {

	// Declarations for getopt
	extern int optind;
	extern char* optarg;
	int ch;
	char* format = "f:hn";
	
	// Variables you'll want to use

	char* filename = "Maumfile";
	bool execute = 1;
	// Part 2.2.1: Use getopt code to take input appropriately.
	while((ch = getopt(argc, argv, format)) != -1) {
		switch(ch) {
			case 'f':
				filename = strdup(optarg);
				break;
			case 'n':
				execute = 0;
				break;
			case 'h':
				maum_usage(argv[0]);
				break;
		}
	}
	argc -= optind;
	argv += optind;

	/* at this point, what is left in argv is the targets that were 
		specified on the command line. argc has the number of them.
		If getopt is still really confusing,
		try printing out what's in argv right here, then just running 
		maum with various command-line arguments. */

	parse_file(filename);

	/* after parsing the file, you'll want to execute all of the targets
		that were specified on the command line, along with their dependencies, etc. */
	return 0;
}
